package pzl.com.mvp.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import pzl.com.mvp.R;
import pzl.com.mvp.model.LoginInteractorImp;
import pzl.com.mvp.model.UserModel;
import pzl.com.mvp.presenter.LoginPresenterImp;
import pzl.com.mvp.presenter.interfaces.ILoginPresenter;
import pzl.com.mvp.view.interfaces.ILoginView;

public class LoginActivity extends AppCompatActivity implements ILoginView, View.OnClickListener {
    private EditText et_name, et_pwd;
    private Button btn_read, btn_save, btn_login;
    private ILoginPresenter iLoginPresenter;

    private void initView() {
        et_name = (EditText) findViewById(R.id.et_name);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        btn_read = (Button) findViewById(R.id.btn_read);
        btn_save = (Button) findViewById(R.id.btn_save);
        btn_login = (Button) findViewById(R.id.btn_login);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        iLoginPresenter = new LoginPresenterImp(this, new LoginInteractorImp(), new UserModel());
        btn_login.setOnClickListener(this);
        btn_read.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }

    @Override
    public void setName(String name) {
        et_name.setText(name);
    }

    @Override
    public String getName() {
        return et_name.getText().toString();
    }

    @Override
    public void setPwd(String pwd) {
        et_pwd.setText(pwd);
    }

    @Override
    public String getPwd() {
        return et_pwd.getText().toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                Toast.makeText(LoginActivity.this, iLoginPresenter.verifyLogin() == true ? "登录成功" : "登录失败", Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_read:
                Toast.makeText(LoginActivity.this, iLoginPresenter.getUser().getName() + ":" + iLoginPresenter.getUser().getPwd(), Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_save:
                iLoginPresenter.saveUser();
                break;
        }
    }
}
